package aor2.cpu.blockoperation;

import aor2.components.AlushiftOperation;
import aor2.components.Signal;

public enum AlushiftSignal {
	nop(AlushiftOperation.nop), inc(AlushiftOperation.inc), dec(
			AlushiftOperation.dec), not(AlushiftOperation.not), asr(
			AlushiftOperation.asr), trans(AlushiftOperation.trans), add(
			AlushiftOperation.add), sub(AlushiftOperation.sub), or(
			AlushiftOperation.or), shl(AlushiftOperation.shl), sgnext(
			AlushiftOperation.sgnext);

	private static AlushiftSignal active = null;
	private AluSignal aluOp;

	private class AluSignal extends Signal {
		private AlushiftOperation operation;

		public AluSignal(AlushiftOperation operation) {
			super(false);
			this.operation = operation;
		}

		@Override
		public void setActive(boolean active) {
			super.setActive(active);
			super.setActive(active);
			if (active) {
				AlushiftXYM1.getInstance().setOperation(operation);
				AlushiftSignal.this.activate();
			} else {
				AlushiftXYM1.getInstance().clearOperation();
				AlushiftSignal.this.deactivate();
			}
		}

		public AlushiftOperation getOperation() {
			return operation;
		}

	}

	static public AlushiftSignal getActive() {
		return active;
	}

	static public AlushiftOperation getActiveOperation() {
		if (active == null)
			return AlushiftOperation.nop;
		else
			return active.aluOp.getOperation();
	}

	static public void clear() {
		if (active != null)
			active.deactivate();
	}

	public boolean isActive() {
		return aluOp.isActive();
	}

	private void setActive(boolean active) {
		if (active)
			activate();
		else
			deactivate();
	}

	private AlushiftSignal(AlushiftOperation operation) {
		this.aluOp = new AluSignal(operation);
	}

	public AluSignal getSignal() {
		return aluOp;
	}

	private void activate() {
		if (active != null) {
			active.setActive(false);
			active = null;
		}
		active = this;
	}

	private void deactivate() {
		if (active == this) {
			active = null;
		}
	}

	static public String info() {
		if (active == null)
			return "operation signal: not active";
		return "operation signal: " + active.name();
	}
}
